/*
 * @Author       : liumour @3d3-Studio
 * @Date         : 2021-02-02 18:26:25
 * @LastEditTime : 2021-03-02 22:37:29
 * @----------------------------------
 * @FilePath     : \Pro_Ui\src\utils\xlsx.js
 * @Description  : 
 */
// 加载处理库
import XLSX from 'xlsx'
import com from '../utils/common'
import df from '../../config/dataFormat'

export default {
    open_file: (filename) => {
        return new Promise((resolve) => {
            var reader = new FileReader();
            reader.onload = file => {
                // 读入工作表
                var workbook = XLSX.read(file.target.result, { type: 'binary' })
                resolve(workbook)
            }
            // 读取文件
            reader.readAsBinaryString(filename)
        })
    },
    open_sheet: (sheet, workbook) => {
        var worksheet = workbook.Sheets[sheet]
        var arr = XLSX.utils.sheet_to_json(worksheet, { header: 1 })
        return arr.length !== 0 ? arr : []
    },
    xlsx_to_table: (arr) => {
        // 获取表头信息
        let cols = arr[0]
        let imp_cols = cols.map(col => {
            col = col ? col : '-'
            return {
                title: col,
                dataIndex: col,
                key: col
            }
        })
        // 去除空值
        imp_cols = imp_cols.filter(f => f)

        // 获取数据
        let data = arr.slice(1)
        data = data.filter(f => f.length !== 0)
        let imp_data = data.map((it, id) => {
            let tmp = { key: id }
            cols.forEach((col, cid) => {
                tmp[col] = it[cid]
            })
            return tmp
        })
        let imp_file_tags = cols
        return { imp_cols, imp_data, imp_file_tags }
    },
    save_tags: (state, dispatch) => {
        console.log('save')
        let list = []
        state.imp_data.forEach((row, rid) => {
            let str = { key: rid }
            for (const key in state.table_bd_tags) {
                str[key] = state.table_bd_tags[key].length > 1 ? state.table_bd_tags[key].map(k => row[k]).filter(i => i).join('； ') : row[state.table_bd_tags[key][0]] ? `${row[state.table_bd_tags[key][0]]}` : null
            }
            if (!str['性别']) {
                let sfz = str['身份证号']
                let ids = sfz.substring(sfz.length - 2, sfz.length - 1)
                let sex = parseInt(ids) % 2 === 1 ? '男' : '女'
                str['性别'] = sex
            }
            str['数据来源'] = state.data_op
            str['操作记录'] = `${state.data_op} ${com.get_format_date()} 由 ${state.user.user} 录入`
            list.push(str)
        })
        dispatch({ type: 'table/save', payload: { foramt_data: list } })
    },
    export_xlsx: (data, filename) => {
        const wb = XLSX.utils.book_new();
        const jsonWs = XLSX.utils.json_to_sheet(data);
        // 将jsonWs 数据放入xlsx文件中的第二个tab，tab名为jsonWs
        XLSX.utils.book_append_sheet(wb, jsonWs, 'sheet1');
        XLSX.writeFile(wb, filename);
    },
    format_list_all: (state) => {

        // var data = state.profile_list
        var data = state.rowSelection_rows


        var keys = df.keys
        delete keys.数据来源
        delete keys.是否下发乡镇跟进
        keys = ['序号', ...keys]
        var format_data = data.map(row => {
            let col = {}
            keys.forEach(key => {
                col[key] = row[key]
            });
            return col
        })
        return format_data
    }
}